package com.jiankangjin.weixinchannel.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.weixinchannel.util.ResponseHandler;

/**
 * 【微信支付】公众号支付接口文档V2.6
 * 接收微信告警通知接口
 */
@Controller
@RequestMapping(value = "/wx")
public class AlarmController {
	private static final Log logger = LogFactory.getLog(AlarmController.class);

	/**
	 * 接收微信告警通知接口
	 */
	@RequestMapping(value = "/alarm", method = RequestMethod.POST)
	@ResponseBody
	public String alarm(HttpServletRequest request, HttpServletResponse response) {
		try {
			ResponseHandler resHandler = new ResponseHandler(request, response);
			logger.info("告警XML参数:" + resHandler.xmlMap);
			String ErrorType = resHandler.xmlMap.get("ErrorType");
			String Description = resHandler.xmlMap.get("Description");
			String AlarmContent = resHandler.xmlMap.get("AlarmContent");
			if (resHandler.isWXsign()) {
				logger.info("微信签名SHA1验证成功");
				if ("1001".equals(ErrorType)) {
					logger.info("消息类型：ErrorType=" + ErrorType + " 发货超时!");
				} else {
					logger.info("消息类型：ErrorType=" + ErrorType + " 未知类型!");
				}
				logger.info("错误描述：Description=" + Description);
				logger.info("错误详情：AlarmContent=" + AlarmContent);
				return "success";
			} else {
				logger.error("微信签名SHA1验证失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fail";
	}
}
